package Task28;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

public class HistoryOfCommand {
	private static int index;
	private static String[] listHistory;

	public static void resetIndex() {
		index = 0;
	}

	public static String getCommandUp() {
		readHistory();
		int num = listHistory.length - index - 1;
		if (index != listHistory.length - 1)
			index++;
		return listHistory[num];
	}

	public static String getCommandDown() {
		if (index == 0)
			return "";
		else {
			readHistory();
			int num = listHistory.length - index;
			if (index != 1)
				index--;
			return listHistory[num];
		}
	}

	private static void readHistory() {
		try {
			List<String> list = new LinkedList<String>();
			FileReader history = new FileReader("log\\history.log");
			BufferedReader reader = new BufferedReader(history);
			String str = "";
			while ((str = reader.readLine()) != null) {
				str = processLineHistory(str);
				list.add(str);
			}
			reader.close();
			listHistory = new String[list.size()];
			list.toArray(listHistory);
			list.clear();
		} catch (IOException e) {
		}
	}

	private static String processLineHistory(String line) {
		String newLine = line.substring(7);
		return newLine;
	}
}